package cn.javabb.core.service.pay;

import cn.hutool.core.lang.Assert;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.entity.pay.GoodsQrDO;
import cn.javabb.core.mapper.pay.GoodsQrMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author javabb
 * @Date 2019/8/14 0014 下午 5:59
 * @Desc
 */
@Service
public class GoodsQrService extends BaseService<GoodsQrDO> {

    @Autowired
    private GoodsQrMapper goodsQrMapper;

    public Page getPage(String page,String size,GoodsQrDO goodsQr) {
        Page p = PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        List<GoodsQrDO> goodsQrList = goodsQrMapper.queryPageBy();
        return p;
    }

    /**
     * 随机取出一条当前无人使用的二维码
     * @return
     */
    public GoodsQrDO getNoUseGoodsQr(Integer goodsId){
        return goodsQrMapper.getNoUseGoodsQr(goodsId);
    }

    public GoodsQrDO getGoodsQrByPayNumber(String payNumber){
        Assert.notNull(payNumber, "支付编码不能为空");
        GoodsQrDO qr = new GoodsQrDO();
        qr.setPayNumber(payNumber);
        return this.queryOne(qr);
    }

    /**
     * 更新状态
     * @param ids
     * @param state
     * @return
     */
    public boolean updateState(String ids, Integer state) {
        List<String> idList = ListUtils.stringToListString(ids);
        Example ex = new Example(GoodsQrDO.class);
        ex.createCriteria().andIn("id", idList);
        GoodsQrDO goodsQr = new GoodsQrDO();
        goodsQr.setState(state);
        return this.goodsQrMapper.updateByExampleSelective(goodsQr, ex)>0?true:false;
    }
}
